import Cookies from 'js-cookie'

export default {
  namespaced: true,
  state: {
    sidebar: {
      opened: Cookies.get('sidebarStatus') ? !!+Cookies.get('sidebarStatus') : true,
      withoutAnimation: false
    },
    device: 'desktop',
    showLoading: false,
    loadingNumber: 0,
  },
  mutations: {
    PLUS_LOADING_NUMBER (state) {
      state.loadingNumber++
    },
    SUBTRACT_LOADING_NUMBER (state) {
      if (state.loadingNumber > 0) {
        state.loadingNumber--
      }
    },
    SET_LOADING_STATE (state, loadingState) {
      state.showLoading = loadingState
    },
  },
  actions: {
    setLoading ({commit, state}) {
      return new Promise(resolve => {
        commit('SUBTRACT_LOADING_NUMBER')
        if (state.loadingNumber <= 0) {
          commit('SET_LOADING_STATE', false)
        }
      })
    }
  }
}
